<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1">
<LINK REL="Start" HREF="index.html">
<LINK REL="Contents" HREF="toc.html">
<LINK REL="Prev" HREF="412WhatyouwillNOTlearninthischapter.html">
<LINK REL="Next" HREF="421UsingtheJpGraphlibrarytosendbackimages.html">
<STYLE TYPE="text/css"><!--
BODY { font-family: serif }
H1 { font-family: sans-serif }
H2 { font-family: sans-serif }
H3 { font-family: sans-serif }
H4 { font-family: sans-serif }
H5 { font-family: sans-serif }
H6 { font-family: sans-serif }
SUB { font-size: smaller }
SUP { font-size: smaller }
PRE { font-family: monospace }
A { text-decoration: none }
--></STYLE>
</HEAD>
<BODY>
<A HREF="toc.html">Contents</A>
<A HREF="412WhatyouwillNOTlearninthischapter.html">Previous</A>
<A HREF="421UsingtheJpGraphlibrarytosendbackimages.html">Next</A>
<HR NOSHADE>
<H2><A NAME="4_2">4.2 How to generate images with PHP</A></H2>
<P> As a general rule each PHP script which generates an image must be
 specified in a separate file which is then called in an HTML &lt;IMG&gt; tag.
 For example, the following HTML excerpt includes the image generated by
 the PHP script in &quot;fig1.php&quot;.</P>
<P><DIV class="phpscript"><CODE><FONT color="#000000"> <FONT color="#0000BB">
&nbsp;</FONT><FONT color="#007700">&lt;</FONT><FONT color="#0000BB">img&nbsp;src</FONT><FONT
color="#007700">=</FONT><FONT color="#DD0000">&quot;fig1.php&quot;&nbsp;</FONT><FONT color="#0000BB">
border</FONT><FONT color="#007700">=</FONT><FONT color="#0000BB">0&nbsp;align</FONT><FONT
color="#007700">=</FONT><FONT color="#0000BB">center&nbsp;width</FONT><FONT color="#007700">
=</FONT><FONT color="#0000BB">300&nbsp;height</FONT><FONT color="#007700">=</FONT><FONT
color="#0000BB">200</FONT><FONT color="#007700">&gt;</FONT><FONT color="#0000BB">
</FONT></FONT></CODE></DIV></P>
<P></P>
<P> Strictly speaking the &quot;align&quot;, &quot;width&quot; and &quot;height&quot; are not
 necessary but helps the browser position the image correctly before the
 image has been fully sent back to the browser.</P>
<P> The library will automatically generate the necessary headers to be
 sent back to the browser so that it correctly recognize the data stream
 received as an image of either PNG/GIF/JPEG format. The browser can
 then correctly decode the image</P>
<P> Observe that you<STRONG> can't</STRONG> return anything else than an
 image from the image script. By definition each HTML page (or more
 correctly each HTTP stream) can only consist of one mime type which is
 determined by the header for that particular stream.</P>
<P> A common mistake is to have a space in the beginning of the image
 script which the HTTP server will send back to the browser. The browser
 now assumes that the data coming back from this script is text since it
 hasn't received an explicit header. When then the image headers get
 sent back to the browser to forewarn the browser of the forthcoming
 image the browser will not like that as it has already assumed the data
 stream was a text stream. The browser will then give the infamous
 &quot;Headers already sent error&quot;.</P>
<P> To include several images together with text on a page you need to
 have a parent page with several &lt;IMG&gt; tags which each refers to an
 image script (or the same image script with GET/POST data).</P>
<HR NOSHADE>
<A HREF="toc.html">Contents</A>
<A HREF="412WhatyouwillNOTlearninthischapter.html">Previous</A>
<A HREF="421UsingtheJpGraphlibrarytosendbackimages.html">Next</A>
</BODY>
</HTML>
